var AliPayimgURL = chrome.extension.getURL("images/QRcodeWisov.jpg");
$("#AliPay").attr("src", AliPayimgURL);
$("#AliPay").css("width","100%");

var WeChatimgURL = chrome.extension.getURL("images/QRcodeWeChat.jpg");
$("#WeChatPay").attr("src", WeChatimgURL);
$("#WeChatPay").css("width","100%");

function convertStr2Boolean(str) {
    if (str === "on") {
        return true;
    }
    else {
        return false;
    }
}

function reload(){
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {greeting: "reload"}, function(response) {
        });
    });
}

chrome.storage.sync.get({index1: 'on', rightAd: 'on', trackBlock: 'off', tuiguangBlock: 'on'}, function (items) {
    index1 = items.index1;
    rightAd = items.rightAd;
    trackBlock = items.trackBlock;
    tuiguangBlock = items.tuiguangBlock;
    document.getElementById("box1").checked = convertStr2Boolean(index1);
    document.getElementById("box2").checked = convertStr2Boolean(rightAd);
    document.getElementById("box3").checked = convertStr2Boolean(trackBlock);
    document.getElementById("box4").checked = convertStr2Boolean(tuiguangBlock);
    document.getElementById('openAll').onclick = function () {
        index1 = "on";
        rightAd = "on";
        trackBlock = "on";
        tuiguangBlock = "on";
        document.getElementById("box1").checked = convertStr2Boolean(index1);
        document.getElementById("box2").checked = convertStr2Boolean(rightAd);
        document.getElementById("box3").checked = convertStr2Boolean(trackBlock);
        document.getElementById("box4").checked = convertStr2Boolean(tuiguangBlock);
        chrome.storage.sync.set({index1: 'on', rightAd: 'on', trackBlock: 'on', tuiguangBlock: 'on'}, function () {
            console.log('保存成功！');
        });
    }
    document.getElementById('index').onclick = function () {
        var change = (index1 === "on") ? "off" : "on";
        chrome.storage.sync.set({index1: change}, function () {
            console.log('保存成功！');
        });
        index1 = change;
        document.getElementById("box1").checked = convertStr2Boolean(change);
        console.log(index1);
        reload();
    }
    document.getElementById('rightAd').onclick = function () {
        var change = (rightAd === "on") ? "off" : "on";
        // localStorage.rightAd = change;
        chrome.storage.sync.set({rightAd: change}, function () {
            console.log('保存成功！');
        });
        rightAd = change;
        document.getElementById("box2").checked = convertStr2Boolean(change);
        console.log(rightAd);
        reload();
    }
    document.getElementById('trackBlock').onclick = function () {
        var change = (trackBlock === "on") ? "off" : "on";
        if(change === "on"){
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
                    // console.log(response.farewell);
                });
            });
        }
        chrome.storage.sync.set({trackBlock: change}, function () {
            console.log('保存成功！');
        });
        trackBlock = change;
        document.getElementById("box3").checked = convertStr2Boolean(change);
        console.log(trackBlock);
    }
    document.getElementById('tuiguangBlock').onclick = function () {
        var change = (tuiguangBlock === "on") ? "off" : "on";
        chrome.storage.sync.set({tuiguangBlock: change}, function () {
            console.log('保存成功！');
        });
        tuiguangBlock = change;
        document.getElementById("box4").checked = convertStr2Boolean(change);
        console.log(tuiguangBlock);
        reload();
    }

});

$(document).ready(function() {
    $("#donate").click(function () {

        if ($("#donate").attr("state") === "hide") {
            $("#QRcode").slideDown("fast");
            $("#donate").text("收起 ▲");
            $("#donate").attr("state", "show")
        }
        else if ($("#donate").attr("state") === "show") {
            $("#QRcode").slideUp("fast");
            $("#donate").text("支持作者 ▼");
            $("#donate").attr("state", "hide")
        }
    });

});